package com.cqtd.menu.pojo;

import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.Expose;
import com.google.gson.reflect.TypeToken;

public class TreeNode implements java.io.Serializable {
	private static final long serialVersionUID = 1000985342961726445L;
	@Expose
	private String id = "";
	@Expose
	private String text = "";// 树节点名称
	@Expose
	private String iconCls = "";// 前面的小图标样式
	private Boolean checked = false;// 是否勾选状态
	private Map<String, Object> attributes;// 其他参数
	@Expose
	private List<TreeNode> children;// 子节点
	@Expose
	private String expanded = "true";// 是否展开(true,false)
	@Expose
	private String buttons = "";// menuID^buttonId
	@Expose
	private String leaf = "false";

	public static void main(String[] args) {

		// Initialize gson builder
		Gson gson = new GsonBuilder().serializeNulls()
				.excludeFieldsWithoutExposeAnnotation().create();

		// Set up childNode1
		TreeNode childNode1 = new TreeNode();
		childNode1.setButtons("4^Add,5^Modify,6^Delete,7^Refresh");
		childNode1.setExpanded("false");
		childNode1.setIconCls("icon-menu");
		childNode1.setId("1");
		childNode1.setLeaf("true");
		childNode1.setText("MenuManage");

		// Set up childNode2
		TreeNode childNode2 = new TreeNode();
		childNode2.setButtons("0^Add,1^Modify,2^Delete,3^Refresh");
		childNode2.setExpanded("false");
		childNode2.setIconCls("icon-menu");
		childNode2.setId("2");
		childNode2.setLeaf("true");
		childNode2.setText("StoreManage");

		// Set up parentNode
		TreeNode parentNode = new TreeNode();
		parentNode.setId("0");
		parentNode.setLeaf("false");
		parentNode.setText("SystemManage");
		List<TreeNode> myTmpList = Lists.newArrayList();
		myTmpList.add(childNode1);
		myTmpList.add(childNode2);
		parentNode.setChildren(myTmpList);

		List<TreeNode> myList = Lists.newArrayList();
		myList.add(parentNode);

		// serialization
		String myEncode = gson.toJson(myList);
		System.out.println(myEncode);

		// deserialization
		Type fooType = new TypeToken<List<TreeNode>>() {
		}.getType();
		List<TreeNode> myDecode = gson.fromJson(myEncode, fooType);
		System.out.println(myDecode);

	}

	public String getLeaf() {
		return leaf;
	}

	public void setLeaf(String leaf) {
		this.leaf = leaf;
	}

	public String getButtons() {
		return buttons;
	}

	public void setButtons(String buttons) {
		this.buttons = buttons;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public Map<String, Object> getAttributes() {
		return attributes;
	}

	public void setAttributes(Map<String, Object> attributes) {
		this.attributes = attributes;
	}

	public List<TreeNode> getChildren() {
		return children;
	}

	public void setChildren(List<TreeNode> children) {
		this.children = children;
	}

	public String getIconCls() {
		return iconCls;
	}

	public void setIconCls(String iconCls) {
		this.iconCls = iconCls;
	}

	public String getExpanded() {
		return expanded;
	}

	public void setExpanded(String expanded) {
		this.expanded = expanded;
	}

	public Boolean getChecked() {
		return checked;
	}

	public void setChecked(Boolean checked) {
		this.checked = checked;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

}
